<?php

namespace app\uniapp\controller;

use app\model\store\GoodsReply;
use app\uniapp\service\GoodsCateService;
use app\uniapp\service\GoodsService;
use app\uniapp\service\UniReqService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Request;
use think\response\Json;

class GoodsController extends UniController
{

    protected array $middleware = [];

    private GoodsService $goodsSer;

    protected function initialize()
    {
        $this->goodsSer = GoodsService::instance();

    }

    /**
     * 不分页列表数据
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function list(): Json
    {
        $param = $this->param();
        $list = $this->goodsSer
            ->setWhereCate($param['cate_id']??0)
            ->setWhereTag($param['tag_id']??0)
            ->query()
            ->limit($param['limit']??10)->select();

        return jsonSuccess([
            'items' => $list,
        ]);
    }

    public function skus(): Json
    {
        $goodsId = $this->param('goods_id');
        $skus = GoodsService::instance()->getSkusByGoodsId($goodsId);
        return jsonSuccess([
            'skus' => $skus,
        ]);
    }

    /**
     * 商品详情
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function find(): Json
    {
        $id = $this->param('id');
        return jsonSuccess([
            'info' => GoodsService::instance()->getDetailById($id)
        ]);
    }


    /** 分页查询
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function paginate(Request $request): Json
    {
        $cateId = $request->param('cate_id', 0);
        $key = trim($this->param('key'));
        $where = [];
        if ($key) $where[] = ['title','like', '%'.$key.'%'];

        $list = GoodsService::instance()->setWhereCate($cateId)->setWhere($where)->query()->paginate(20);
        return jsonSuccess([
            'items' => $list->items(),
            'total' => $list->total(),
        ]);
    }

    /**
     * 商品评论分页列表
     * @throws DbException
     */
    public function replyPaginate(Request $request): Json
    {
        $goodsId = $request->param('goods_id');
        $limit = $request->param('limit', 20);
        $list = GoodsReply::where('goods_id', $goodsId)->order('create_time desc')->filter(function ($v){
                $v->createTimeStr = substr($v->create_time, 0, 10);
                $v->imageArr = miExplode($v->images);
            })
            ->paginate($limit);

        return jsonSuccess([
            'items' => $list->items(),
            'total' => $list->total(),
        ]);

    }

    /**
     * 商品分类
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function cates(): Json
    {
        $list = GoodsCateService::instance()->select();
        $tree = GoodsCateService::instance()->childrenTree($list);
        return jsonSuccess([
            'items' => $tree,
        ]);
    }

}
